﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Marbles.Core.Connectors;
using Marbles.Core.Content.Sites;

namespace Marbles.Core.Content.Banners
{
    public class Banner
    {

        public Int32 ID { get { return _id; } }
        public string SiteId { get { return _siteid; } }
        public string ImageUrl { get { return _imageurl; } set { _imageurl = value; } }
        public string NavigateUrl { get { return _navigateurl; } set { _navigateurl = value; } }
        public string AlternateText { get { return _alternatetext; } set { _alternatetext = value; } }
        public string Keyword { get { return _keyword; } set { _keyword = value; } }
        public Int32 Impressions { get { return _impressions; } set { _impressions = value; } }
        public Int32 Width { get { return _width; } set { _width = value; } }
        public Int32 Height { get { return _height; } set { _height = value; } }
        public bool Active { get { return _active; } set { _active = value; } }
        internal Int32 BannerId { set { _id = value; } }

        private bool _active = false;
        private Int32 _id = 0;
        private string _imageurl = string.Empty;
        private string _navigateurl = string.Empty;
        private string _alternatetext = string.Empty;
        private string _keyword = string.Empty;
        private Int32 _impressions = 0;
        private Int32 _width = 0;
        private Int32 _height = 0;
        private string _siteid = string.Empty;

        public Banner()
        {
            _id = 0;
            _siteid = string.Empty;
            _active = false;
            _imageurl = string.Empty;
            _navigateurl = string.Empty;
            _alternatetext = string.Empty;
            _keyword = string.Empty;
            _impressions = 0;
            _width = 0;
            _height = 0;
        }

        internal Banner(DataRow row)
        {
            _id = Convert.ToInt32(row["id"]);
            _imageurl = row["imageurl"].ToString();
            _navigateurl = row["navigateurl"].ToString();
            _alternatetext = row["alternatetext"].ToString();
            _keyword = row["keyword"].ToString();
            _impressions = Convert.ToInt32(row["impressions"]);
            _width = Convert.ToInt32(row["width"]);
            _height = Convert.ToInt32(row["height"]);
            _active = Convert.ToBoolean(row["active"]);
            _siteid = row["siteid"].ToString();
        }

        public static Banner CreateNew(SiteDefinition Web)
        {
            Banner b = new Banner();
            b._siteid = Web.SiteId;
            return b;
        }

        public void Delete()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("DELETE FROM Banners WHERE siteid = @siteid AND id = @id");
            cmd.AddParameter("@siteid", this.SiteId);
            cmd.AddParameter("@siteid", this.ID);
            dbc.ExecuteCommand(cmd);
        }

        public void Update()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = null;
            int bannerid = this.ID;
            if (this.ID > 0)
            {
                Delete();
            }

            if (bannerid == 0) { bannerid = BannerCollection.CountBanners(this.SiteId) + 1; }

            cmd = new Command("INSERT INTO Banners (siteid, id, imageurl, navigateurl, alternatetext, keyword, impressions, width, height, active)  VALUES (@siteid, @id, @imageurl, @navigateurl, @alternatetext, @keyword, @impressions, @width, @height, @active)");
            cmd.AddParameter("@siteid", SiteId);
            cmd.AddParameter("@id", bannerid);
            cmd.AddParameter("@imageurl", ImageUrl);
            cmd.AddParameter("@navigateurl", NavigateUrl);
            cmd.AddParameter("@alternatetext", AlternateText);
            cmd.AddParameter("@keyword", Keyword);
            cmd.AddParameter("@impressions", Impressions);
            cmd.AddParameter("@width", Width);
            cmd.AddParameter("@height", Height);
            cmd.AddParameter("@active", Active);
            dbc.ExecuteCommand(cmd);
        }
    }
}
